<?php
declare(strict_types=1);

namespace app\controller\admin;

use app\BaseController;
use app\model\MessageSetting;
use think\facade\Db;
use think\Request;

class MessageSettingController extends BaseController
{
    /**
     * 获取消息配置列表
     */
    public function list()
    {
        $where = [];
        $type = $this->request->param('type',0);
        if($type){  
            $where['type'] = $type;
        }
       
        try {
            $list = MessageSetting::field('id,type,scene,scene_name,create_time,update_time')
                ->where($where)
                ->order('id', 'desc')
                ->select();
            return $this->success('获取成功',$list);
            
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 更新消息配置
     */
    public function update(Request $request, $id)
    {
        Db::startTrans();
        try {
            $data = $request->post();
            echo 111;die;
            // 验证数据
            $validate = new \app\validate\MessageSettingValidate();
            if (!$validate->check($data)) {
                return json(['code' => 1, 'msg' => $validate->getError()]);
            }

            $setting = MessageSetting::find($id);
            if (!$setting) {
                return json(['code' => 1, 'msg' => '消息配置不存在']);
            }

            $setting->save([
                'type' => $data['type'],
                'scene' => $data['scene']
            ]);

            Db::commit();
            return json(['code' => 0, 'msg' => '更新成功']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
} 